package com.ruoyi.sycs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.sycs.domain.bo.BaFanganDetailBo;
import com.ruoyi.sycs.domain.vo.BaFanganDetailVo;
import com.ruoyi.sycs.domain.BaFanganDetail;
import com.ruoyi.sycs.mapper.BaFanganDetailMapper;
import com.ruoyi.sycs.service.IBaFanganDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 方案明细Service业务层处理
 *
 * @author zyb
 * @date 2024-10-17
 */
@RequiredArgsConstructor
@Service
public class BaFanganDetailServiceImpl implements IBaFanganDetailService {

    private final BaFanganDetailMapper baseMapper;

    /**
     * 查询方案明细
     */
    @Override
    public BaFanganDetailVo queryById(Long fanganId){
        return baseMapper.selectVoById(fanganId);
    }

    /**
     * 查询方案明细列表
     */
    @Override
    public TableDataInfo<BaFanganDetailVo> queryPageList(BaFanganDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaFanganDetail> lqw = buildQueryWrapper(bo);
        Page<BaFanganDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询方案明细列表
     */
    @Override
    public List<BaFanganDetailVo> queryList(BaFanganDetailBo bo) {
        LambdaQueryWrapper<BaFanganDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaFanganDetail> buildQueryWrapper(BaFanganDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaFanganDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getFanganId() != null, BaFanganDetail::getFanganId, bo.getFanganId());
        lqw.eq(StringUtils.isNotBlank(bo.getFieldKey()), BaFanganDetail::getFieldKey, bo.getFieldKey());
        lqw.eq(StringUtils.isNotBlank(bo.getFieldLabel()), BaFanganDetail::getFieldLabel, bo.getFieldLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getXsdws()), BaFanganDetail::getXsdws, bo.getXsdws());
        return lqw;
    }

    /**
     * 新增方案明细
     */
    @Override
    public Boolean insertByBo(BaFanganDetailBo bo) {
        BaFanganDetail add = BeanUtil.toBean(bo, BaFanganDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFanganId(add.getFanganId());
        }
        return flag;
    }

    /**
     * 修改方案明细
     */
    @Override
    public Boolean updateByBo(BaFanganDetailBo bo) {
        BaFanganDetail update = BeanUtil.toBean(bo, BaFanganDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaFanganDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除方案明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
